Online PHP Function(s){ #Test PHP functions online!; }

  •  
 

 

get_ip_address()

(Custom function by John)

get_ip_address -- Retrieves Ip adres from user and checkes for proxy server.

Description

(string) get_ip_address()

This simple function checkes whether the uses is behind a (transparent) proxy, and returns the found IP adres.

Function

<?php
function get_ip_address()
    {
        if (isset(
$_SERVER)) {
            if (isset(
$_SERVER["HTTP_X_FORWARDED_FOR"]) && ip2long($_SERVER["HTTP_X_FORWARDED_FOR"]) !== false) {
                
$ipadres $_SERVER["HTTP_X_FORWARDED_FOR"];
            } elseif (isset(
$_SERVER["HTTP_CLIENT_IP"])  && ip2long($_SERVER["HTTP_CLIENT_IP"]) !== false) {
                
$ipadres $_SERVER["HTTP_CLIENT_IP"];
            } else {
                
$ipadres $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (
getenv('HTTP_X_FORWARDED_FOR') && ip2long(getenv('HTTP_X_FORWARDED_FOR')) !== false) {
                
$ipadres getenv('HTTP_X_FORWARDED_FOR');
            } elseif (
getenv('HTTP_CLIENT_IP') && ip2long(getenv('HTTP_CLIENT_IP')) !== false) {
                
$ipadres getenv('HTTP_CLIENT_IP');
            } else {
                
$ipadres getenv('REMOTE_ADDR');
            }
        }
        return 
$ipadres;
    }
?>


Result:

107.23.176.162

PHP call:

<?php
echo get_ip_address();
?>


Edit, save or share this code in the Sandbox



 

 

 


Comments

 
 
      © 2019 OnlinePHPFunctions.com | Disclaimer |       PHP versions: 7.2.4, 7.1.0, 7.0.14, 7.0.5, 7.0.4, 7.0.3, 7.0.2, 7.0.1, 5.6.29, 5.6.20, 5.6.19, 5.6.18, 5.6.17, 5.6.2, 5.5.34, 5.5.33, 5.5.32, 5.5.31, 5.5.18, 5.5.5, 5.5.0.a6, 5.5.0.a.5, 5.5.0.a.2, 5.4.34, 5.4.21, 5.4.13, 5.4.12, 5.4.11, 5.4.10, 5.4.9, 5.4.8, 5.4.7, 5.4.6, 5.4.5, 5.4.4, 5.4.3, 5.4.2, 5.4.1, 5.4.0, 5.3.29, 5.3.27, 5.3.23, 5.3.22, 5.3.21, 5.3.20, 5.3.19, 5.3.18, 5.3.17, 5.3.16, 5.3.15, 5.3.14, 5.3.13, 5.3.12, 5.3.11, 5.3.10, 5.3.2, 5.3.1, 5.3.0, 5.2.17, 5.2.16, 5.1.6, 5.1.5, 5.0.5, 5.0.4, 4.4.9